package Model;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/*be
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author thuydao
 */
public class M_Pro {
   
    private  int masp;
    private  String tensp;
    private  String nhacc;
    private  String madmsp;
    private  String mota;
    private  float giavon;
    private  float giaban;
    private  int khuyenmai;
    private  String trangthai;
    private  String hinhanh;
    
            
            
    public M_Pro(){}
    
    private Connection conn;
    
      public void connect(){
         try {
            String userName = "root"; // root là mặc định của MySQL.
            String passWord = "";
            String url = 
            "jdbc:mysql://localhost:3306/handmade?useUnicode=true&characterEncoding=UTF-8"; 
           
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            setConn(DriverManager.getConnection(url, userName, passWord));
            }
            catch (Exception e) {
                System.out.println("Diver sai");
            }
        
        }
      public M_Pro(int masp, String tensp, String nhacc, String madmsp, String mota, float giavon, float giaban, int khuyenmai, String trangthai, String hinhanh)
      {
          this.masp=masp;
          this.tensp=tensp;
          this.nhacc = nhacc;
          this.madmsp = madmsp;
          this.mota = mota;
          this.giavon = giavon;
          this.giaban = giaban;
          this.khuyenmai = khuyenmai;
          this.trangthai = trangthai;
          this.hinhanh = hinhanh;
          
                 
      }

    /**
     * @return the masp
     */
    public int getMasp() {
        return masp;
    }

    /**
     * @param masp the masp to set
     */
    public void setMasp(int masp) {
        this.masp = masp;
    }

    /**
     * @return the tensp
     */
    public String getTensp() {
        return tensp;
    }

    /**
     * @param tensp the tensp to set
     */
    public void setTensp(String tensp) {
        this.tensp = tensp;
    }

    /**
     * @return the nhacc
     */
    public String getNhacc() {
        return nhacc;
    }

    /**
     * @param nhacc the nhacc to set
     */
    public void setNhacc(String nhacc) {
        this.nhacc = nhacc;
    }

    /**
     * @return the madmsp
     */
    public String getMadmsp() {
        return madmsp;
    }

    /**
     * @param madmsp the madmsp to set
     */
    public void setMadmsp(String madmsp) {
        this.madmsp = madmsp;
    }

    /**
     * @return the mota
     */
    public String getMota() {
        return mota;
    }

    /**
     * @param mota the mota to set
     */
    public void setMota(String mota) {
        this.mota = mota;
    }

    /**
     * @return the giavon
     */
    public float getGiavon() {
        return giavon;
    }

    /**
     * @param giavon the giavon to set
     */
    public void setGiavon(float giavon) {
        this.giavon = giavon;
    }

    /**
     * @return the giaban
     */
    public float getGiaban() {
        return giaban;
    }

    /**
     * @param giaban the giaban to set
     */
    public void setGiaban(float giaban) {
        this.giaban = giaban;
    }

    /**
     * @return the khuyenmai
     */
    public int getKhuyenmai() {
        return khuyenmai;
    }

    /**
     * @param khuyenmai the khuyenmai to set
     */
    public void setKhuyenmai(int khuyenmai) {
        this.khuyenmai = khuyenmai;
    }

    /**
     * @return the trangthai
     */
    public String getTrangthai() {
        return trangthai;
    }

    /**
     * @param trangthai the trangthai to set
     */
    public void setTrangthai(String trangthai) {
        this.trangthai = trangthai;
    }

    /**
     * @return the hinhanh
     */
    public String getHinhanh() {
        return hinhanh;
    }

    /**
     * @param hinhanh the hinhanh to set
     */
    public void setHinhanh(String hinhanh) {
        this.hinhanh = hinhanh;
    }

    /**
     * @return the conn
     */
    public Connection getConn() {
        return conn;
    }

    /**
     * @param conn the conn to set
     */
    public void setConn(Connection conn) {
        this.conn = conn;
    }
    
   
    
    
    public boolean insert_Pro(String tensp, String nhacc, String madmsp, String mota, float giavon, float giaban, int khuyenmai, String trangthai, String hinhanh){
            connect();
        
        try{
                
            
            String sql_insert_KH = "INSERT INTO sanpham (TenSP, NhaCC, MaDMSP, MoTa, GiaVon, GiaBan, KhuyenMai, TrangThai, HinhAnh) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

            PreparedStatement stmt = conn.prepareStatement(sql_insert_KH);
            stmt.setString(1, tensp);
            stmt.setString(2, nhacc);
            stmt.setString(3, madmsp);
            stmt.setString(4, mota);
            stmt.setFloat(5, giavon);
            stmt.setFloat(6, giaban);
            stmt.setInt(7, khuyenmai);
            stmt.setString(8, trangthai);
            stmt.setString(9, hinhanh);
            
            int val = stmt.executeUpdate();
            if(val>0)
            return true;
            }
            catch (SQLException s){
                System.out.println("SQL statement is not executed! <br>"+s.getMessage());
            //result = "SQL statement is not executed! <br>"+s.getMessage();
            }
       return false;
          
   }
    //int masp, String tensp, String nhacc, String madmsp, String mota, float giavon, float giaban, int khuyenmai, String trangthai, String hinhanh
     public M_Pro[] show_Pro(){
            connect();
            ResultSet rs=null;
            M_Pro[] result=null;
            try{
            String sql_show_Pro = "select *from sanpham"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_show_Pro);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
     
    public boolean delete_tablePro(){
            connect();
        
        try{
                
            
            String sql_delete = "DELETE FROM sanpham "; 

            PreparedStatement stmt = conn.prepareStatement(sql_delete);
                    
            int val = stmt.executeUpdate();
            if(val>0)
            return true;
            }
            catch (SQLException s){
                System.out.println("SQL statement is not executed! <br>"+s.getMessage());
            //result = "SQL statement is not executed! <br>"+s.getMessage();
            }
       return false;
          
   }
    
    
        public boolean delete_Pro(){
            connect();
            
            
           //DELETE FROM `danhmucsp` WHERE 1
          try{
            String sql_delete_TK = "Delete from sanpham Where MaSP=?"; 
            
            PreparedStatement stm = conn.prepareStatement(sql_delete_TK);
            stm.setInt(1, masp);
            
            int val= stm.executeUpdate();
            stm.close();
            conn.close();
            if(val>0)
                return true;
            
            
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
          return false;
   }
        
   public boolean update_Pro(){
            connect();
            
            
          //int masp, String tensp, String nhacc, String madmsp, String mota, float giavon, float giaban, int khuyenmai, String trangthai, String hinhanh
          try{
            String sql_update_Pro = "update SANPHAM set TenSP=?, NhaCC=?, MaDMSP=?, MoTa=?, GiaVon=?, GiaBan=?, KhuyenMai=?, TrangThai=?, HinhAnh=? where MaSP=?"; 
            PreparedStatement stm = conn.prepareStatement(sql_update_Pro);
            stm.setString(1, tensp);
            stm.setString(2, nhacc);
            stm.setString(3, madmsp);
            stm.setString(4, mota);
            stm.setFloat(5, giavon);
            stm.setFloat(6, giaban);
            stm.setFloat(7, khuyenmai);
            stm.setString(8, trangthai);
            stm.setString(9, hinhanh);
            stm.setInt(10, masp);
        
            int val= stm.executeUpdate();
            stm.close();
            conn.close();
           if(val>0)
                return true;
        
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
          return false;
   } 
   public M_Pro[] searchTensp(String searchTensp){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql_search_KH = "select *from sanpham where TenSP like '%" + searchTensp + "%'"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_search_KH);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
   
    public M_Pro[] searchDMSP(String searchDMSP){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql_search_KH = "select *from sanpham where MaDMSP like '%" + searchDMSP + "%'"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_search_KH);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    
    public M_Pro[] searchNhacc(String searchNhacc){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql_search_KH = "select *from sanpham where NhaCC like '%" + searchNhacc + "%'"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_search_KH);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    
    public M_Pro[] searchMota(String searchMota){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql_search_KH = "select *from sanpham where MoTa like '%" + searchMota + "%'"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_search_KH);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    
    public M_Pro[] searchTrangthai(String searchTrangthai){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql_search_KH = "select *from sanpham where TrangThai like '%" + searchTrangthai + "%'"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_search_KH);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    
    public M_Pro[] showDMSP(String dmsp){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql = "select *from sanpham where MaDMSP='" + dmsp + "'"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    //hiện thị theo giá
    
    public M_Pro[] showDuoi50(){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql = "select *from sanpham where GiaBan<50000"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    public M_Pro[] show50150(){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql = "select *from sanpham where GiaBan>=50000 and GiaBan<=150000"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
     public M_Pro[] showTren150(){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql = "select *from sanpham where GiaBan>150000"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    
    
    
    // Sắp xếp
    public M_Pro[] sortDESC(){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql_sort = "SELECT * FROM sanpham ORDER BY KhuyenMai DESC"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_sort);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    public M_Pro[] sortASC(){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql_sort = "SELECT * FROM sanpham ORDER BY KhuyenMai ASC"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_sort);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    
     public M_Pro[] sortPmtDESC(){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql_sort = "SELECT * FROM sanpham ORDER BY KhuyenMai DESC LIMIT 8"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_sort);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    public M_Pro[] sortIdDESC(){
          connect();
            ResultSet rs = null;
            M_Pro[] result=null;
            try{
            String sql_sort = "SELECT * FROM sanpham ORDER BY MaSP DESC LIMIT 8"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_sort);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int masp = rs.getInt("MaSP");
                String tensp = rs.getString("TenSP");
                String nhacc = rs.getString("NhaCC");
                String madmsp = rs.getString("MaDMSP");
                String mota = rs.getString("MoTa");
                Float giavon = rs.getFloat("GiaVon");
                Float giaban = rs.getFloat("GiaBan");
                int khuyenmai = rs.getInt("KhuyenMai");
                String trangthai = rs.getString("TrangThai");
                String hinhanh = rs.getString("HinhAnh");
                
                M_Pro cus = new M_Pro(masp, tensp, nhacc, madmsp, mota, giavon, giaban, khuyenmai, trangthai, hinhanh);
                list.add(cus);
                
                
            }
            result = new M_Pro[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    
}
